import common from "shared/component/common/common";
import { ContainerService } from "shared/component/container/ContainerService";
import EventBinder from "shared/component/event/EventBinder";
import Log4R from "shared/component/log/Log4R";
import { ColiGroup } from "shared/enums/ColiGroup";
const loginfo = Log4R.getLog('info');

export class CharacterServiceImpl extends ContainerService<Ctn.CharacterService, Ctn.CharacterEntity> implements Ctn.CharacterService {

    /** 创建玩家进入角色后触发 */
    syncPlayerCharacter(param: Ctn.character.PlayerCharacterEntity, session: Ctn.Session) {
        const character = param.character;
        if (!character) {
            throw ('no character')
        }
        // loginfo?.info('syncPlayerCharacter', '----------->')
        let isPlayer = false
        if (character === game.GetService('Players').LocalPlayer.Character) {
            isPlayer = true
            character.UpperTorso.CollisionGroup = ColiGroup.playerHumanRootPart
        }
        this.setCharacter(param)
        this.creatCharacterEventBinder.callBack(param)
        while (!character.FindFirstChild("HumanoidRootPart")) {
            task.wait(1)
        }

        common.onNoParent(character, () => {
            this.removeCharacterEventBinder.callBack(param, isPlayer);
        })

    }
    loadCharacter(characterEntity: Ctn.character.PlayerCharacterEntity): void {
        throw ("Method not implemented.");
    }

    /** 角色新增监听 */
    protected creatCharacterEventBinder = new EventBinder<(characterEntity: Ctn.character.PlayerCharacterEntity) => void>()
    /** 当玩家添加角色时 */
    onCharacterAdd = this.creatCharacterEventBinder.bind()
    /** 角色新增监听 */
    protected removeCharacterEventBinder = new EventBinder<(characterEntity: Ctn.character.PlayerCharacterEntity, isPlayer: boolean) => void>()
    /** 角色模型添加 */
    onCharacterRemove = this.removeCharacterEventBinder.bind()
    protected serverCalls: (keyof this)[] = [];
    protected serviceName: keyof Ctn.Services = "characterService";
    public run(): void {

    }

}